package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.GoodsCategory;
import com.wholesale.beans.GoodsInfo;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.GoodsListAdapter;

public class GoodsListLayout extends LinearLayout implements
		OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private Button newButton ;
	private Button lowButton ;
	private Button highButton ;
	private RelativeLayout loadingLayout ;
	
	private GoodsCategory parent ;
	private GoodsListAdapter listAdapter;
	private GoodsListListAsync categoriesAsync;
	private String shortBy ="lastUpdate";
    private  Drawable newon,newoff,lowtohighon,lowtohighoff,hightolowon,hightolowoff;
	// 构造函数
	public GoodsListLayout(Context context, final ViewFlipper viewFlipper,String leftText,
			GoodsCategory goodsCategory) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		this.parent = goodsCategory ;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.goodslist, this);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText(leftText);
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.GONE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText(goodsCategory.getCatName());
		loadingLayout = (RelativeLayout)baseView.findViewById(R.id.loadingLayout);
		
		newButton = (Button)baseView.findViewById(R.id.newBtn);
		newButton.setOnClickListener(this);
		lowButton = (Button)baseView.findViewById(R.id.lowBtn);
		lowButton.setOnClickListener(this);
		highButton = (Button)baseView.findViewById(R.id.highBtn);
		highButton.setOnClickListener(this);
		initList() ;
		// 加载数据
		categoriesAsync = new GoodsListListAsync(context);
		categoriesAsync.execute(true);
		
		
		newon=this.getResources().getDrawable(R.drawable.details_segment_1_on);
		newoff=this.getResources().getDrawable(R.drawable.details_segment_1_off);
		lowtohighon=this.getResources().getDrawable(R.drawable.details_segment_2_on);
		lowtohighoff=this.getResources().getDrawable(R.drawable.details_segment_2_off);
		hightolowon=this.getResources().getDrawable(R.drawable.details_segment_3_on);
		hightolowoff=this.getResources().getDrawable(R.drawable.details_segment_3_off);
	}

	private void initList(){
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		bodyList.setSelector(R.drawable.details_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);
		listAdapter = new GoodsListAdapter(context, bodyList);
		listAdapter.setAsyncLoadData(true);
		bodyList.setAdapter(listAdapter);
	}
	
	// ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			GoodsDetailLayout goodsDetailLayout = new GoodsDetailLayout(context,
					 viewFlipper, parent.getCatName(),listAdapter.getItem(arg2));
			viewFlipper.addView(goodsDetailLayout);
			viewFlipper.showNext();
		}
	};

	class GoodsListListAsync extends
			AsyncTask<Boolean, Integer, ArrayList<GoodsInfo>> {
		ProgressDialog pdialog;
		long goodsCount = 1;
		boolean desc;
		public GoodsListListAsync(Context context) {
//			pdialog = new ProgressDialog(context);
//			pdialog.setTitle("Loading...");
//			pdialog.setMessage("Please wait...");
//			pdialog.setCancelable(true);
//			pdialog.setButton("cancel", new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, int i) {
//					dialog.cancel();
//					GoodsListListAsync.this.cancel(true);
//				}
//			});
//			pdialog.show();
		}

		@Override
		protected void onPreExecute() {
			listAdapter.stop();
			listAdapter.clear();
			listAdapter.notifyDataSetChanged();
			loadingLayout.setVisibility(View.VISIBLE);
		}
		
		@Override
		protected ArrayList<GoodsInfo> doInBackground(Boolean... params) {
			desc = params[0];
			goodsCount = WebServiceClient.getGoodsListByCatPageCount((short)parent.getCatId());
			return WebServiceClient.getGoodsListByCat(parent.getCatId(),1, shortBy, desc);
		}
		@Override
		protected void onPostExecute(ArrayList<GoodsInfo> result) {
			listAdapter.clear();
			listAdapter.addItems(result);
			listAdapter.setPageNum(GoodsListAdapter.ACTION_GOODSLISTBYCARTID, parent.getCatId(), (int)goodsCount,shortBy, desc);
			listAdapter.notifyDataSetChanged();
//			pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
			break;
		case R.id.newBtn:
			shortBy = "lastUpdate" ;
			
			newButton.setBackgroundDrawable(newon);
			lowButton.setBackgroundDrawable(lowtohighoff);
			highButton.setBackgroundDrawable(hightolowoff);
			if(categoriesAsync!=null){
				categoriesAsync.cancel(true);
			}
			categoriesAsync = new GoodsListListAsync(context);
			categoriesAsync.execute(true);
			break;
		case R.id.lowBtn:
			shortBy = "shopPrice" ;
			newButton.setBackgroundDrawable(newoff);
			lowButton.setBackgroundDrawable(lowtohighon);
			highButton.setBackgroundDrawable(hightolowoff);
			if(categoriesAsync!=null){
				categoriesAsync.cancel(true);
			}
			categoriesAsync = new GoodsListListAsync(context);
			categoriesAsync.execute(false);
			break;
		case R.id.highBtn:
			shortBy = "shopPrice" ;
			newButton.setBackgroundDrawable(newoff);
			lowButton.setBackgroundDrawable(lowtohighoff);
			highButton.setBackgroundDrawable(hightolowon);
			if(categoriesAsync!=null){
				categoriesAsync.cancel(true);
			}
			categoriesAsync = new GoodsListListAsync(context);
			categoriesAsync.execute(true);
			break;
		default:
			break;
		}

	}
}
